﻿using AllEncryption;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPrj
{
    internal class SMProgram
    {
        public void Test()
        {
            /*

            var data = "中华人民共和国万岁";
            var utf8Bytes = Encoding.UTF8.GetBytes(data);
            var hexStrign = Hex.Encode(utf8Bytes);



            Console.WriteLine("*************SM2****************");
            var pair = SM2Util.GenerateKeyPair();

            pair.PrivateKey = "f2239272674a62e63ea56b6bca260b2b40bf9d9215e0043ebee092413d77df5c";
            pair.PublicKey = "04116176183480c574072af57c6b6c87b404dd9a4465ddcadcd75f7bdf87a721e9d95ba62c57ab2ccc842633f4ca34979d787188f0d1b40691edfbc461bb3e7225";



            Console.WriteLine("公钥:" + pair.PublicKey);
            Console.WriteLine("私钥:" + pair.PrivateKey);



            var encryptData = SM2Util.Encrypt(data, pair.PublicKey);
            Console.WriteLine("密文:" + encryptData);

            //encryptData = "9a1ba40c46cdd4a4a822648039d17e949cd480f3bc03477402dd4775d5836a8688388f1fbd10e5d3436af67a80ac48dbd6d38c62438130fbf33bff871c886c0c52d5e4e7fc375123108bd8cd8a5ae4b808075ab11d9b2078b6e9978ca7f99ceff781f03d6a817955088c56";
            var decryptData = SM2Util.Decrypt(encryptData, pair.PrivateKey);

            Console.WriteLine("解密后:" + decryptData);
            var dd = Encoding.UTF8.GetBytes(data);


            //var signer = SM2Util.GenerateSM2Signature(data,pair.PrivateKey );
            var signer = SM2Util.Sign(pair.PrivateKey, Encoding.UTF8.GetBytes(data));

            Console.WriteLine(Hex.ToHexString(signer));
            var signerHex = Hex.Decode("3045022100f72a9b1d1cb6bfa2aacf26678a4b74981634e8b73498f737298530e63c4ba80902203acc66cc1a6f35c10bff9b54132893977848d0de8cf0fa9b01d6f557f67cb7bb");

            //Console.WriteLine("检验结果"+SM2Util.VerifySM2Signature(data,signerHex,pair.PublicKey));
            Console.WriteLine("检验结果" + SM2Util.VerifySign(pair.PublicKey, Encoding.UTF8.GetBytes(data), signerHex));

            
            Console.WriteLine("*************SM3****************");

            string key = "au6b227z3fwd";
            string hexStr = SM3Util.SignatureBySM3(data + key, key);
            Console.WriteLine("密文:" + hexStr);
            Console.WriteLine("校验结果"+ SM3Util.VerifyBySM3(data+key,key,hexStr));
        
            Console.WriteLine("*************SM4****************");
            data = "hello,world";

            var key = "0123456789abcdef";
            string iv = "";
            SM4Util.GenerateKeyHex(out key, out iv);
            // 502945e1f5c2cc6eb14fc6e1f51dc94d

            data = "中华人民共和国万岁";

            key = "244a6568e4a9f55e7d10c956121b6538";

            SM4Util.Test(data, key);
            */
        }
    }
}
